# -*-coding:utf-8 -*-

from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
import time,os
try:
    from ..config import ROOT_PATH
except :
    from config import ROOT_PATH





'''
dict_data:读取的配置
numpage:页数
功能描述：打开指定页面 执行翻页
chrome = 65 
chromedriver = 2.38
'''
def selenium_get(dict_data,num):
    str_url = dict_data["listen_url"]
    str_js = dict_data["selenium_js_page"]
    #num_page = int(dict_data["publicity_pages"])
    #1.创建Chrome浏览器对象，这会在电脑上在打开一个浏览器窗口
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument('--headless')
    chrome_options.add_argument('--disable-gpu')

    str_chromedriver_path = os.path.join(ROOT_PATH,"govpublicity","spider","chromedriver.exe")
    browser = webdriver.Chrome(chrome_options=chrome_options,executable_path =str_chromedriver_path)
    #2.通过浏览器向服务器发送URL请求
    browser.get(str_url)
    time.sleep(3)
   
    str_js = str_js % str(num)
    #js = "__doPostBack('MoreinfoListsearch1$Pager','2')" 
    print (type(str_js) , str_js)
    browser.execute_script(str_js) 
    time.sleep(3)
    return (browser.page_source)
    #print ("*"*10)
    #selenium_response(browser,dict_data)
    #publicity_text_list_pre = browser.find_elements_by_xpath(dict_data["publicity_text_xpath"])
    #print (publicity_text_list_pre)

    #3.刷新浏览器
    #browser.refresh()
